import { loginApi } from "@/apis/Login";
import 'element-plus/theme-chalk/el-message.css'
import { defineStore } from "pinia";
import { ref } from "vue";
import { useCartStore } from "./cart";
import { concatCartList } from '@/apis/Cart'
export const useUserStore = defineStore('user',() => {
    const cartStore = useCartStore()
    const userInfo = ref({})
    //获取用户信息
    const getUserInfo = async ({account,password}) => {
        const {data:{result}} = await loginApi({
            account,
            password
        })
        userInfo.value = result
        await concatCartList(
            cartStore.cartList.map(item => {
                return {
                    skuId:item.skuId,
                    selected:item.selected,
                    count:item.count
                }
            })
        )
        cartStore.getCart()
    }
    //删除用户信息
    const removeUserInfo = () => {
        userInfo.value = {}
        cartStore.clearCartList()
    }
    return {
        userInfo,
        getUserInfo,
        removeUserInfo
    }
},{
    persist:true
})